'#include once "../inc/tile_engine.bi"

#include once "../inc/gfx.bi"
#include once "../inc/map.bi"
#include once "ext/graphics.bi"

extern bomber as ext.gfx.sprite ptr
extern curr_map as map
extern console as uinteger

const as integer move_speed = 48

sub mainloop()

   dim as integer play_x = 288,play_y = 240,frm = 0,play_dir = 1,moving = 0
   dim as integer world_x = 6,world_y = 5,put_x, put_y
   ' 6 * 48 = 288 and 5 * 48 = 240 
   dim as double tim = timer,fps
   
   do
      sleep 1,1
      fps += 1
 
      frm = frm mod 64
      if (frm = 0) then moving = 0
      if (moving = 0) then frm = 0
      
      if ((timer - tim) < 0.001667) then continue do
      
      world_x = (play_x / Tile_Size)
      world_y = (play_y / Tile_Size)
      
      'if(world_x + 6 >= curr_map.max_x) then world_x = curr_map.max_x - 6
      'if(world_y + 5 >= curr_map.max_y) then world_y = curr_map.max_y - 5
       
      
      screenlock  
      
      
      line(0,0)-(800,600),rgb(0,0,0),bf
      
      drawMap(world_x , world_y)
            
      if (play_x < map_x/2) then
         put_x = play_x
      else
         put_x = map_x/2
      end if
      if (play_y < map_y/2) then
         put_y = play_y
      else
         put_y = map_y/2
      end if         
      
      put(put_x,put_y),bomber[play_dir].GetImage(frm/8),trans
      
      screenunlock
      
      if(multikey(fb.sc_up)) then
         play_dir = SPR_UP
         if ((play_y - move_speed) > 0) then 
            play_y -= move_Speed
         end if
         moving = 1
      elseif(multikey(fb.sc_left)) then
         play_dir = SPR_LEFT
         if ((play_x - move_speed) > 0) then            
            play_x -= move_Speed
         end if
         moving = 1
      elseif(multikey(fb.sc_right)) then 
         play_dir = SPR_RIGHT
         if ((play_x + move_speed) < (curr_map.max_x * Tile_Size)) then 
            play_x += move_Speed
         end if
         moving = 1
      elseif(multikey(fb.sc_down)) then
         play_dir = SPR_DOWN
         if ((play_y + move_speed) < (curr_map.max_y * Tile_Size)) then
            play_y += move_Speed
         end if
         moving = 1
      end if
         
      
      if(not(multikey(Fb.sc_up) or multikey(Fb.sc_left) or multikey(Fb.sc_right) or multikey(Fb.sc_down))) then
         moving = 0
      end if 
      
      if moving = 1 then frm += 1
      tim = timer
      locate 1,1 : ? fps
      fps = 0
      
      ' Print #console, "Drawing map with size" & .max_x & "x" & .max_y
      ' Temp Debugging shit
      
      print #console, " Cam x : " & world_x & " Cam y : " & world_y 
      
      '------------
      

   loop until multikey(fb.sc_escape)
   
end sub

